package sddz.foodmanage.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.zhy.autolayout.AutoFrameLayout;
import com.zhy.autolayout.AutoLinearLayout;

import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import sddz.foodmanage.R;
import sddz.foodmanage.base.BaseActivity;
import sddz.foodmanage.fragment.HomeFragment;
import sddz.foodmanage.fragment.InpurchasingFragment;
import sddz.foodmanage.fragment.MyFragment;
import sddz.foodmanage.fragment.OrderformFragment;
import sddz.foodmanage.utils.ChangeFragment;
import sddz.foodmanage.utils.GlobalParms;
import sddz.foodmanage.utils.SpfUtils;
import sddz.foodmanage.utils.UpdateManager;

/**
 * @Date:2019/7/8
 * @Author:lbb
 */

public class MainNewActivity extends BaseActivity {
    @BindView(R.id.one_img)
    ImageView oneImg;
    @BindView(R.id.one_lin)
    AutoLinearLayout oneLin;
    @BindView(R.id.two_img)
    ImageView twoImg;
    @BindView(R.id.two_lin)
    AutoLinearLayout twoLin;
    @BindView(R.id.three_img)
    ImageView threeImg;
    @BindView(R.id.three_lin)
    AutoLinearLayout threeLin;
    @BindView(R.id.four_img)
    ImageView fourImg;
    @BindView(R.id.four_lin)
    AutoLinearLayout fourLin;
    @BindView(R.id.fl_container)
    AutoFrameLayout flContainer;
    @BindView(R.id.tv_one)
    TextView tvOne;
    @BindView(R.id.tv_tow)
    TextView tvTow;
    @BindView(R.id.tv_three)
    TextView tvThree;
    @BindView(R.id.tv_four)
    TextView tvFour;
    private HomeFragment homefragment;
    private InpurchasingFragment inpurchaingfragment;
    private MyFragment myfragment;
    private OrderformFragment orderformfragment;

    private FragmentManager manager;
    private FragmentTransaction transaction;
    //声明一个long类型变量：用于存放上一点击“返回键”的时刻
    private long mExitTime;
    public static  int a=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_new);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.e("starta",a+"");
    }

    private void initData() {
        Log.e("SpfUtils.getUserData(mActivity).getType()",SpfUtils.getUserData(mActivity).getType()+"");
        if(SpfUtils.getUserData(mActivity).getType()==2) {

                GlobalParms.setFragmentSelected(new ChangeFragment() {
                    @Override
                    public void changge(int postion) {
                        if(mActivity!=null&&!mActivity.isFinishing()) {
                            Log.e("SpfUtils.getUserData(mActivity).getType()postion", SpfUtils.getUserData(mActivity).getType() + "");
                            setSwPage(postion);
                        }
                    }
                });
        }
    }



    private void initView() {
        //获取FragmentManager对象
        manager = getSupportFragmentManager();
        //获取FragmentTransaction对象
        transaction = manager.beginTransaction();
        //先隐藏所有的Fragment
        hideFragments(transaction);
//        setSwPage(0);
        //管理员
        if(SpfUtils.getUserData(mActivity).getType()==1){//我的，订单
            oneLin.setVisibility(View.GONE);
            twoLin.setVisibility(View.GONE);
            threeLin.setVisibility(View.VISIBLE);
            fourLin.setVisibility(View.VISIBLE);
            setSwPage(2);//默认是从第3个显示
        }else if(SpfUtils.getUserData(mActivity).getType()==2){//中队 商品浏览 采购中 订单 我的
            oneLin.setVisibility(View.VISIBLE);
            twoLin.setVisibility(View.VISIBLE);
            threeLin.setVisibility(View.VISIBLE);
            fourLin.setVisibility(View.VISIBLE);
            setSwPage(0);

        }else if(SpfUtils.getUserData(mActivity).getType()==3){//供应商 商品浏览 订单 我的
            oneLin.setVisibility(View.VISIBLE);
            twoLin.setVisibility(View.GONE);
            threeLin.setVisibility(View.VISIBLE);
            fourLin.setVisibility(View.VISIBLE);
            setSwPage(0);
        }
        UpdateManager updateManager = new UpdateManager(mActivity);
        updateManager.isUpdate();
    }

    @OnClick({R.id.one_lin, R.id.two_lin, R.id.three_lin, R.id.four_lin})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.one_lin:
                setSwPage(0);
                break;
            case R.id.two_lin:
                setSwPage(1);
                break;
            case R.id.three_lin:
                setSwPage(2);
                break;
            case R.id.four_lin:
                setSwPage(3);
                break;
        }
    }


    public void setSwPage(int i) {
        //获取FragmentManager对象
        manager = getSupportFragmentManager();
        //获取FragmentTransaction对象
        transaction = manager.beginTransaction();
        //先隐藏所有的Fragment

        hideFragments(transaction);
        switch (i) {
            case 0:
                twoImg.setSelected(false);
                threeImg.setSelected(false);
                fourImg.setSelected(false);
                oneImg.setSelected(true);
                tvOne.setTextColor(getResources().getColor(R.color.title_color));
                tvTow.setTextColor(getResources().getColor(R.color.backgray));
                tvThree.setTextColor(getResources().getColor(R.color.backgray));
                tvFour.setTextColor(getResources().getColor(R.color.backgray));
                if (homefragment == null) {
                    homefragment = new HomeFragment();
                    transaction.add(R.id.fl_container, homefragment);
                } else {
                    //如果微信对应的Fragment已经实例化，则直接显示出来
                    transaction.show(homefragment);
                }
                break;
            case 1:
                oneImg.setSelected(false);
                threeImg.setSelected(false);
                fourImg.setSelected(false);
                twoImg.setSelected(true);
                tvOne.setTextColor(getResources().getColor(R.color.backgray));
                tvTow.setTextColor(getResources().getColor(R.color.title_color));
                tvThree.setTextColor(getResources().getColor(R.color.backgray));
                tvFour.setTextColor(getResources().getColor(R.color.backgray));
                if (inpurchaingfragment == null) {
                    inpurchaingfragment = new InpurchasingFragment();
                    transaction.add(R.id.fl_container, inpurchaingfragment);
                } else {
                    //如果微信对应的Fragment已经实例化，则直接显示出来
                    //获取当前时间
                    Date date = new Date();
                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    String sim = dateFormat.format(date);
                    inpurchaingfragment.setTvDeliverTime(sim);
                    transaction.show(inpurchaingfragment);
                }
                break;
            case 2:
                oneImg.setSelected(false);
                twoImg.setSelected(false);
                fourImg.setSelected(false);
                threeImg.setSelected(true);
                tvOne.setTextColor(getResources().getColor(R.color.backgray));
                tvTow.setTextColor(getResources().getColor(R.color.backgray));
                tvThree.setTextColor(getResources().getColor(R.color.title_color));
                tvFour.setTextColor(getResources().getColor(R.color.backgray));
               if(SpfUtils.getUserData(mActivity).getType()==2){
                   if (orderformfragment == null) {
                       orderformfragment = new OrderformFragment();
                       transaction.add(R.id.fl_container, orderformfragment);
                   } else {
                       //如果微信对应的Fragment已经实例化，则直接显示出来
                       transaction.remove(orderformfragment);
                       orderformfragment = new OrderformFragment();
                       transaction.add(R.id.fl_container, orderformfragment);
                   }

               }else {
                   if (orderformfragment == null) {
                       orderformfragment = new OrderformFragment();
                       transaction.add(R.id.fl_container, orderformfragment);
                   } else {
                       //如果微信对应的Fragment已经实例化，则直接显示出来
                       transaction.show(orderformfragment);
                   }
               }
                break;
            case 3:
                oneImg.setSelected(false);
                twoImg.setSelected(false);
                threeImg.setSelected(false);
                fourImg.setSelected(true);
                tvOne.setTextColor(getResources().getColor(R.color.backgray));
                tvTow.setTextColor(getResources().getColor(R.color.backgray));
                tvThree.setTextColor(getResources().getColor(R.color.backgray));
                tvFour.setTextColor(getResources().getColor(R.color.title_color));
                if (myfragment == null) {
                    myfragment = new MyFragment();
                    transaction.add(R.id.fl_container, myfragment);

                } else {
                    //如果微信对应的Fragment已经实例化，则直接显示出来
                    transaction.show(myfragment);
                }
                break;
        }
        transaction.commitAllowingStateLoss();
    }

    //将四个的Fragment隐藏
    private void hideFragments(FragmentTransaction transaction) {
        if (homefragment != null) {
            transaction.hide(homefragment);
        }
        if (inpurchaingfragment != null) {
            transaction.hide(inpurchaingfragment);
        }
        if (orderformfragment != null) {
            transaction.hide(orderformfragment);
        }
        if (myfragment != null) {
            transaction.hide(myfragment);
        }
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //判断用户是否点击了“返回键”
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //与上次点击返回键时刻作差
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                //大于2000ms则认为是误操作，使用Toast进行提示
                Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                //并记录下本次点击“返回键”的时刻，以便下次进行判断
                mExitTime = System.currentTimeMillis();
            } else {
                //小于2000ms则认为是用户确实希望退出程序-调用System.exit()方法进行退出
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }



}
